from wtforms import StringField, Form, IntegerField, DecimalField
from wtforms.validators import regexp, InputRequired, ValidationError, Regexp, Length, EqualTo, NumberRange, AnyOf
import time
from utils import redis_cach
import hashlib


class BaseForm(Form):
    def get_error(self):
        message = self.errors.popitem()[1][0]
        return message


class DetailForm(BaseForm):
    goods_id = StringField(validators=[InputRequired(message='请选择商品')])
    # token = StringField(validators=[InputRequired(message='请选择商品')])


class ShippingForm(BaseForm):
    shipping_name = StringField(validators=[InputRequired(message='请输入收货人姓名')])
    shipping_phone = StringField(validators=[Regexp(r"1[23456789]\d{9}", message='请输入正确格式的手机号码！')])
    shipping_address = StringField(validators=[InputRequired(message='请输入收货地址')])


class ShoppingForm(DetailForm):
    num = IntegerField(validators=[NumberRange(1, 1000000, message='数量格式错误')])


class SearchForm(BaseForm):
    goods_name = StringField(validators=[InputRequired(message='请输入商品名字')])
    price_status = StringField()   # 价格排序
    sales_status = StringField()   # 销量排序


class OrderHistoryForm(BaseForm):
    goods_name = StringField()
    t_type = IntegerField(validators=[AnyOf([0, 1, 2], message='请选择类型')])
    page = IntegerField(validators=[InputRequired(message='请选择分页')])


class BuyForm(BaseForm):    # 挂买订单
    goods_id = StringField(validators=[InputRequired(message='请选择商品')])
    num = IntegerField(validators=[NumberRange(1, 1000000, message='数量格式错误')])
    price = DecimalField(validators=[InputRequired(message='请输入正确的价格')])
    # t_type = IntegerField(validators=[AnyOf([1, 2], message='请选择类型')])


class SellForm(BaseForm):   # 挂卖订单
    bath_id = IntegerField(validators=[InputRequired(message='请选择订单')])
    num = IntegerField(validators=[NumberRange(1, 1000000, message='数量格式错误')])
    price = DecimalField(validators=[InputRequired(message='请输入正确的价格')])

